export function listToTree(list, parentId, level = 0, map) {
  const prop = {
    id: 'id',
    parentId: 'parentId',
    children: 'children',
    level: 'level',
    order: 'order',
    path: 'path',
    ...map
  };

  const temp = Object.create(null),
    tree = [];
  list.forEach(item => {
    temp[item[prop.id]] = Object.assign({}, item);
  });
  for (const key in temp) {
    const item = temp[key];
    const pId = item[prop.parentId];
    if (pId === parentId) {
      item[prop.level] = level;
      item[prop.path] = [item[prop.id]];
      tree.push(item);
    } else {
      const parent = temp[pId];
      if (parent) {
        if (!parent[prop.children]) {
          parent[prop.children] = [];
        }
        const path = (parent[prop.path] || []).concat(item[prop.id]);

        item[prop.level] = parent[prop.level] + 1;
        item[prop.path] = path;
        parent[prop.children].push(item);
      }
    }
  }
  return tree;
}
